#ifndef PATH_NODE_MANAGER
#define PATH_NODE_MANAGER

#include <string>
#include <vector>
#include "Defines.cpp"
#include "UrlConverter.cpp"

class PathNodeManager
{
private:
    std::vector<std::string> _nodeNames;

public:
    PathNodeManager(std::string urlPath)
    {
        // "/home/deepin/Desktop/CppWorkspace/WebRoot";
        // "home/deepin//Desktop/CppWorkspace/WebRoot";
        // "home/deepin//Desktop/CppWorkspace/WebRoot/";
        std::string name = "";
        for (int i = 0; i < urlPath.length(); i++)
        {
            if (urlPath[i] == '/')
            {
                if (name.length() > 0)
                {
                    _nodeNames.push_back(name);
                    name.clear();
                }
            }
            else
            {
                name += urlPath[i];
            }
        }
        if (name.length() > 0)
        {
            _nodeNames.push_back(name);
        }
    }

    std::string GetNameByIndex(int index)
    {
        if (index < 0 || index >= _nodeNames.size())
        {
            PrintLine("index 越界, index [%d], node size [%d]", index, _nodeNames.size());
            return "";
        }
        return _nodeNames[index];
    }
    std::string GetNameByIndex_UrlMode(int index)
    {
        return UrlConverter::ConvertToUrl(GetNameByIndex(index));
    }

    std::string GetPath(int index)
    {
        std::string path = "/";
        if (index < 0 || index >= _nodeNames.size())
        {
            PrintLine("index 越界, index [%d], node size [%d]", index, _nodeNames.size());
            return path;
        }
        for (int i = 0; i <= index; i++)
        {
            path += _nodeNames[i];
            path += "/";
        }
        return path;
    }

    std::string GetPath_UrlMode(int index)
    {
        std::string path = "/";
        if (index < 0 || index >= _nodeNames.size())
        {
            PrintLine("index 越界, index [%d], node size [%d]", index, _nodeNames.size());
            return path;
        }
        for (int i = 0; i <= index; i++)
        {
            path += GetNameByIndex_UrlMode(i);
            path += "/";
        }
        return path;
    }

    int GetLength()
    {
        return _nodeNames.size();
    }
};

#endif